package com.haidong.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.haidong.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class JdbcConfig {
    // 从配置文件中获取需要的参数
    @Value("${driver}")
    private String driver;
    @Value("${url}")
    private String url;
    @Value("${userName}")
    private String userName;
    @Value("${password}")
    private String password;

    @Bean
    public DataSource dataSource(BookDao bookDao){
        //  包扫描已经扫描到bookDao的bean了,就会自动配置进来了
        System.out.println(bookDao);

        // 不使用DataSource因为内部没setter方法
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }
}
